/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package threads;

import filters.ByteStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import own_packets.Protocol;

/**
 *
 * @author Hnmtuan
 */
public class DownloadConnection implements Runnable {

    private String _host;
    private Integer _port;
    private Socket _ownSocket;
    private InputStream _in;
    private OutputStream _output;
    private ObjectOutputStream _objOutput;
    private String _fileName;
    private String _urlName;

    public String getFileName() {
        return _fileName;
    }

    public void setFileName(String _fileName) {
        this._fileName = _fileName;
    }

    public String getHost() {
        return _host;
    }

    public void setHost(String _host) {
        this._host = _host;
    }

    public Integer getPort() {
        return _port;
    }

    public void setPort(Integer _port) {
        this._port = _port;
    }

    public DownloadConnection(String host, Integer port, String fileName, String urlName) {
        _host = host;
        _port = port;
        _fileName = fileName;
        _urlName = urlName;
    }

    public String getUrlName() {
        return _urlName;
    }

    public void setUrlName(String _urlName) {
        this._urlName = _urlName;
    }

    public void run() {
        try {
            // Connection to server with hostname, portname
            _ownSocket = new Socket(_host, _port);
            // Create outputStream to send data to a following server
            _output = _ownSocket.getOutputStream();
            // Send request type as test connection to a following server
            if (_output == null) {
                System.out.println("Connect to a peer server is failed");
                return;
            }

            // Create packet to send a following server
            Protocol myPacket = new Protocol();
            myPacket.setIsRequest(Boolean.TRUE);
            myPacket.setFileName(_urlName);
            myPacket.setRequestType(Protocol.RequestType.DOWNLOAD_REQUEST);

            // Forward packet to a following server
            ByteStream.toStream(_output, myPacket);

            // Create inputStream to received data from a following server
            _in = _ownSocket.getInputStream();

            // Received Response from a following server
            Protocol objRS = ByteStream.toProtocol(_in);

            if (objRS == null) {
                System.out.println("Server Peer not response");
            } else {
                if (!objRS.getIsDownload()) {
                    System.out.println(String.format("Server Peer Response : %s", objRS.getMessages()));
                } else {
                        File file = new File(_fileName);
                        ByteStream.toFile(_in, file);
                        System.out.println(String.format("Download file %s from server peer success", _fileName));
                }
            }
        } catch (ClassNotFoundException ex) {
            System.out.println(String.format("Failed DownloadConnection Peer : %s", ex.getMessage()));
        } catch (UnknownHostException ex) {
            System.out.println(String.format("Failed DownloadConnection Peer : %s", ex.getMessage()));
        } catch (IOException ex) {
            System.out.println(String.format("Failed DownloadConnection Peer : %s", ex.getMessage()));
        } finally {
            try {
                _ownSocket.close();
            } catch (IOException ex) {
                System.out.println(String.format("Failed DownloadConnection Peer : %s", ex.getMessage()));
            }
        }
    }
}
